package com.ruoyi.app.controller;

import com.ruoyi.app.interceptor.JWTUtil;
import com.ruoyi.app.interceptor.SessionUtil;
import com.ruoyi.cms.domain.CrmMember;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.employee.domain.CrmEmployee;
import com.ruoyi.message.domain.MessageZan;
import com.ruoyi.message.service.IMessageInfoService;
import com.ruoyi.message.service.IMessageZanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 赞记录Controller
 * 
 * @author hct
 * @date 2022-04-14
 */
@RestController
@RequestMapping("/app/message/zan")
public class AppMessageZanController extends BaseController
{
    @Autowired
    private IMessageZanService messageZanService;

    @Autowired
    private IMessageInfoService messageService;

    /**
     * 新增赞记录
     */
    @PostMapping(value = "/add")
    public AjaxResult add(MessageZan messageZan)
    {
        CrmMember crmEmployee=SessionUtil.getLoginUser();
        messageZan.setMemberId(crmEmployee.getId());
        int zancount= messageZanService.getByMemberIdAndMessageId(crmEmployee.getId(),messageZan.getMessageId());
        if(zancount>0){
            messageService.updateIsZanSubById(messageZan.getMessageId());
            messageZanService.delMemberIdAndMessageId(crmEmployee.getId(),messageZan.getMessageId());
            return AjaxResult.success("取消成功");
        }else{
            System.out.println("我的share.........");
            messageZan.setCreateDate(new Date());
            messageZan.setUpdateDate(new Date());
            int i=messageZanService.insertMessageZan(messageZan);
            if(i>0) {
                messageService.updateIsZanAddById(messageZan.getMessageId());
                return AjaxResult.success("点赞成功");
            }else {
                return AjaxResult.success("点赞失败");

            }
        }
    }

}
